package dao

import (
	"usplatformserver/internal/model"
	"usplatformserver/pkg/app"
)

/**
 * @brief 插入数据
 * @param organType-检查类型
 * @param fileName-文件名
 * @param filePath-文件路径
 * @param fileSize-文件大小
 * @return 文件ID，错误信息
 */
func (d *Dao) InsertUploadFile(organType int, fileName, filePath, fileSize string) (int, error) {
	info := model.UploadFileInfo{
		FileName:  fileName,
		FilePath:  filePath,
		FileSize:  fileSize,
		OrganType: organType,
		Status:    model.ToBeAnalyzed,
	}

	return info.InsertUploadFile(d.engine)
}

func (d *Dao) DeleteUploadFile(fileId int) error {
	info := model.UploadFileInfo{}
	return info.DeleteUploadFile(d.engine, fileId)
}

/**
 * @brief 获取上传文件信息列表
 * @param option-搜索条件
 * @param beginTime-起始时间
 * @param endTime-结束时间
 * @param pageOffset-当前页起始数据偏移
 * @param pageSize-页码
 * @return 错误信息
 */
func (d *Dao) GetUploadFileList(organType uint32, option, beginTime, endTime string, page, pageSize int) ([]*model.UploadFileInfo, error) {
	u := model.UploadFileInfo{}
	pageOffset := app.GetPageOffset(page, pageSize)
	return u.List(d.engine, organType, option, beginTime, endTime, pageOffset, pageSize)
}

/**
 * @brief 获取上传文件信息列表总数
 * @param option-搜索条件
 * @param beginTime-起始时间
 * @param endTime-结束时间
 * @return 错误信息
 */
func (d *Dao) CountUploadFile(organType uint32, option, beginTime, endTime string) (int, error) {
	u := model.UploadFileInfo{}
	return u.Count(d.engine, organType, option, beginTime, endTime)
}

/**
 * @brief 文件是否存在
 * @param db-数据库访问实例
 * @param fileName-文件名
 * @return 错误信息
 */
func (d *Dao) IsExistFile(fileName string) (bool, error) {
	u := model.UploadFileInfo{}
	return u.IsExistFile(d.engine, fileName)
}
